package com.petAdoption.manager.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.petAdoption.manager.entity.Pet;
import com.petAdoption.manager.service.IPetService;
import com.petAdoption.manager.util.Result;
import com.petAdoption.manager.vo.PetVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王倩茹
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/pet")
public class PetController {
     @Resource
     private IPetService petService;
     @Resource
     private Result result;


     @GetMapping("/findByName")
     public List<PetVO> findByIdName(String petName){
         List<PetVO> rows = petService.findByName(petName);
         return petService.findByName(petName);
     }

     @DeleteMapping("/deletePet")
      public Result deletePet(Integer petId){
         System.out.println(petId);
         int rows = petService.deletePet(petId);
         if(rows>0){
             return result.success("删除成功",rows);
         }else {
             return result.fail("删除失败");
         }
      }

    @GetMapping("/listPetAll/{pageNum}")
    public Result listPetAll(@PathVariable Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<PetVO> list = petService.findPetAll();
        return result.success(new PageInfo<>(list));
    }

    @GetMapping("/findByNameAll/{pageNum}")
    public Result findByNameAll(String petName,@PathVariable Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<PetVO> list = petService.findByNameAll(petName);
        return result.success(new PageInfo<>(list));
    }

    @PostMapping("/addPet")
    public Result addPet(@RequestBody Pet pet){
         int rows = petService.addPet(pet);
         if(rows>0){
             return result.success("添加成功",rows);
         }else {
             return result.fail("添加失败");
         }
    }


    @PutMapping("/updatePet")
    public Result updatePet(@RequestBody Pet pet){
         int rows = petService.updatePet(pet);
        if(rows>0){
            return result.success("修改成功",rows);
        }else {
            return result.fail("修改失败");
        }
    }

}
